L1-025 正整数A+B
题目 L1-025 正整数A+B
思路分析
代码实现
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
//无语 题目又不说清楚
//a>1||a>1000||b>1||b>1000 均为不合法行为(卡最后两个测试点)
//a,b中有负数的存在,也不合法,题目规定均为正整数
//还有两个卡在输入 输入是含空格的 要用getline读 但是又没换行 只能手动分割 -100 blabla bla...33
bool isLegal(string s) {
if (s.empty()) return false;
if (s.size() > 1 && s[0] == '0') return false;
for (char c : s) {
if (!isdigit(c)) return false;
}
int num = stoi(s);
return num >= 1 && num <= 1000;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
string ex; getline(cin,ex);
string a = "", b = "";
size_t spacePos = ex.find(' ');
if (spacePos != string::npos) {
a = ex.substr(0, spacePos);
b = ex.substr(spacePos + 1);
} else {
a = ex;
b = "";
}
if (!isLegal(a)) a = "?";
if (!isLegal(b)) b = "?";
int sum = 0;
if (a != "?") sum += stoi(a);
if (b != "?") sum += stoi(b);
cout << a << " + " << b << " = ";
if (a == "?" || b == "?") {
cout << "?";
} else {
cout << sum;
}
return 0;
}
同类题型
视频讲解
⬅️ L1-024 后天 🏠 00-天梯赛 ➡️ L1-026 I Love GPLT
💬 评论